<?php
namespace Common\ORG\Tools;

/**
 * 管理工具Http 助手类
 * 
*/
class Http {
    /**
     * Get 请求
     * 
     * @param string $url 访问地址
     * 
     * @rerurn string HTTP请求结果
    */
     public static function get($url)
     {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
            } else {
            return false;
        }
     } 

     /**
      * Post请求
      * 
      * @param string $url              提交地址
      * @param mixed  $data             POST数据
      * @param boolen force_encode      提交表单
      *                                     是:以 application/x-www-form-urlencoded
      *                                     否:multipart/form-data
      * 
      * @return false/string 接口返回结果
      * @param boolen force_encode      提交表单
*                                       是:以 application/x-www-form-urlencoded
*                                       否:multipart/form-data
     */
     public static function post($url, $data, $force_encode = true)
     {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        if (is_array($data)) {
            foreach ($data as &$value) {
                if (is_string($value) && stripos($value, '@') === 0 && class_exists('CURLFile', FALSE)) {
                    $value = new \CURLFile(realpath(ltrim($value, '@')));
                }
            }
            if ($force_encode)
                $data = http_build_query($data);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $retdata = curl_exec($ch);
        curl_close($ch);
        if ($retdata) {
            return $retdata;
        }
        return false;
     }
}

?>